package com.globallogic.vkulyk.SSHTool;

import com.jcraft.jsch.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ActionExecutor {
	private String host = "";
	private int port = 0;
	private String user = "";
	private String password = "";
	private final Logger logger = LoggerFactory.getLogger(ActionExecutor.class);

	public ActionExecutor(String hostArg, int portArg, String userArg,
			String passwordArg) {
		host = hostArg;
		port = portArg;
		user = userArg;
		password = passwordArg;
	}

	public Session openSession() {
		logger.debug("Opening SSH session..");
		Session session = null;
		try {
			JSch jsch = new JSch();
			session = jsch.getSession(user, host, port);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return session;
	}

	public void executeInExistingSession(Session sessionArg) {
	}

	public void closeSession(Session session) {
		if (session != null) {
			logger.debug("Closing session..");
			session.disconnect();
		}
	}

}
